package org.wgc.study.controller;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wgc.study.config.MybatisPlusConfig;
import org.wgc.study.config.ReconciliationJobConfig;

import java.util.Date;

;

/***
 *@title CallBatchJobController
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-08-04 15:06
 **/
@RestController
@RequestMapping("/batch")
public class TestJobController {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    MybatisPlusConfig mybatisPlusConfig;

    @GetMapping("testJob")
    public void testJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        //    后置参数：使用JobParameters中绑定参数 addLong  addString 等方法

        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(mybatisPlusConfig.myJob(), jobParameters);
        BatchStatus status = run.getStatus();
        System.out.println("status = " + status);
    }


    @Autowired
    Job myJobNew;

    @GetMapping("testJobNew")
    public void testJobNew(@RequestParam("authorId") String authorId) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

        JobParameters jobParametersNew = new JobParametersBuilder().addLong("timeNew", System.currentTimeMillis())
                .addString("authorId", authorId)
                .toJobParameters();
        jobLauncher.run(myJobNew, jobParametersNew);

    }

   @Autowired
    ReconciliationJobConfig reconciliationJobConfig;

    @GetMapping("testJobNew2")
    public void testJobNew2() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        JobParameters params = new JobParametersBuilder()
                .addString("inputFile", "classpath:trans-20230520.csv")
                .addDate("runDate", new Date())
                .toJobParameters();

         jobLauncher.run(reconciliationJobConfig.bankReconciliationJob(), params);

    }
}
